/*global jQuery, $, resultMap*/

var rateme = rateme || {};

/**
 * Converts a string "lat,lng" to object
 * @method strToLatLngObj
 * @param {String} strLatLng
 */
function strToLatLngObj(strLatLng) {
	var arrLatLng = strLatLng.split(',');
	return {
		'lat' : arrLatLng[0],
		'lng' : arrLatLng[1]
	};
}

/**
 * Takes int value as parameter and prints out html stars (5 stars max is presumed)
 * @method printStars
 * @param {int} howmany
 */
function printStars(howmany) {
	var i = 0,
		stars = '';
	
	if (isNaN(howmany)) {
		howmany = 0;
	}
	
	if (howmany > 5) {
		howmany = 5;
	} else if (howmany < 0) {
		howmany = 0;
	}
	
	while (i < 5) {
		if (i < howmany) {
			//stars = stars + '&#9733;';
			stars = stars + '<span class="star">&nbsp;</span>';
		} else {
			//stars = stars + '&#9734;';
			stars = stars + '<span class="star empty-star">&nbsp;</span>';
		}
		++i;
	}
	
	return stars;
}

/**
 * Takes a string value such as //"tag:api.qype.com,2009-10-06:/places/927823" and returns /places/927823
 * @method getPlaceId
 * @param {String} qypeResultPlaceId
 */
function getPlaceId(qypeResultPlaceId) {
	var a = null,
		b = null;
		
	a = qypeResultPlaceId.split(':');
	b = a[a.length - 1];
	return b.split('/')[2];	
}

/**
 * Creates html for result list
 * @author Thomas Junghans, Pirmin Schürmann
 * @class resultlist
 * @namespace rateme
 * @requires jQuery, rateme
 */
function ResultList(data, selector) {
	// List
	$(selector + ' .list').html('');
	$(selector + ' .message').html('');
	
	var tmpHtml = null,
		i = 0,
		placeid = 0;
	
	
	if (data.total_entries === 0) {
		tmpHtml = [];
		tmpHtml.push('<div>');
		tmpHtml.push('No entries found.');
		tmpHtml.push('</div>');
		tmpHtml = tmpHtml.join('');
    
		$(selector + ' .message').append(tmpHtml).removeClass('hide');
					
	} else {
		$(selector).children('.result').removeClass('hide');
		tmpHtml = [];
		tmpHtml.push('<ul class="lean iphonelist">');
		for (i = 0; i < data.results.length; i++) {
			placeid = getPlaceId(data.results[i].place.id);
			
			tmpHtml.push('<li>');
			tmpHtml.push('<a href="#place-details?id=' + placeid + '" qypeid="' + placeid + '">' + data.results[i].place.title + '</a>');
			
			if (!isNaN(data.results[i].place.distance)) {
				tmpHtml.push('<span class="distance">(' + Math.round(data.results[i].place.distance * 100) / 100 + ' km)</span>');	
			}
			
			tmpHtml.push('<div class="mod rating">' + printStars(data.results[i].place.average_rating) + '</div>');
			tmpHtml.push('</li>');
			
	    }
		
		tmpHtml.push('</ul>');
		
		tmpHtml = tmpHtml.join('');
    
		$(selector + ' .list').append(tmpHtml);
		
		$(selector).trigger('resultlistcomplete', {'pageid' : selector});
	}
}

/**
 * Creates html for result details
 * @author Thomas Junghans, Pirmin Schürmann
 * @class resultdetails
 * @namespace rateme
 * @requires jQuery, rateme
 */
(function ($) {
	function ResultDetails(data) {
		this.place = data.place;
		this.address = data.place.address;
	}
	
	ResultDetails.prototype = {
		'toString' : function () {
			var tmpHtml = [];
			tmpHtml.push('<div class="place">');
			tmpHtml.push('<div class="title">' + this.place.title + '</div>');
			tmpHtml.push('<div class="rating">' + printStars(this.place.average_rating) + '</div>');
			tmpHtml.push('<div class="address">');
			tmpHtml.push(this.address.street + ' ' + this.address.housenumber);
			tmpHtml.push('<br/>');
			tmpHtml.push(this.address.country_code + ' ' + this.address.postcode);
			tmpHtml.push('<br/>');
			tmpHtml.push(this.address.city);
			tmpHtml.push('</div>');
			tmpHtml.push('<div class="phone">' + this.place.phone + '</div>');
			if (this.place.url.length !== '') {
				tmpHtml.push('<div class="url"><a href="' + this.place.url + '">' + this.place.url + '</a></div>');	
			}
			tmpHtml.push('<div class="map"><a href="#search-place-map" point="' + this.place.point + '">View Map</a></div>');
			tmpHtml.push('</div>');
			
			return tmpHtml.join('');
		},
		
		'getPoint' : function () {
			return this.place.point;
		}
	};
	
	rateme.resultdetails = ResultDetails;
}(jQuery));

